package com.syh.jn.scm.domain.upstream.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.supplier.entity.base.upstream.Upstream;
import com.syh.jn.scm.domain.supplier.repository.base.upstream.UpstreamReadRepository;
import com.syh.jn.scm.domain.upstream.repository.system.UpAccountReadRepository;
import com.syh.jn.scm.dto.upstream.system.upAccount.UpAccountMto;
import com.syh.jn.scm.dto.upstream.system.upAccount.UpAccountSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 运营账号管理
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class UpAccount extends AbstractSimpleAggregate<Long> {
  /**
   * 登录账号
   */
  private String loginAccount;
  /**
   * 登录密码
   */
  private String loginPassword;
  /**
   * 手机账号id
   */
  private Long loginPhoneId;
  /**
   * 数据归属公司ID
   */
  private Long businessEntityId;
  /**
   * 姓名
   */
  private String name;
  /**
   * 电话
   */
  private String telephone;
  /**
   * 终端系统类型
   */
  private String terminalType;
  /**
   * 角色id
   */
  private Long roleId;
  /**
   * 是否管理员 1:是
   */
  private Short admin;
  /**
   * 使用情况:-1冻结, 1启用
   */
  private Short enable;

  public UpAccount(Long id) {
    this.id = id;
  }

  public UpAccount(UpAccountSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    UpstreamReadRepository readRepository = (UpstreamReadRepository) SpringContextUtils.getBean(UpstreamReadRepository.class);
    UpAccountReadRepository accountReadRepository = (UpAccountReadRepository) SpringContextUtils.getBean(UpAccountReadRepository.class);
    Upstream upstream = readRepository.getOne(Wrapper.build().append(Restrictions.eq("id", sto.getBusinessEntityId())));
    //生成带code的账号
    StringBuffer upAccount = new StringBuffer(sto.getLoginAccount()).append("@").append(upstream.getCode());
    sto.setLoginAccount(upAccount.toString());
    //检查账号是否已存在
    Long count = accountReadRepository.getCountByAccountName(sto.getLoginAccount());
    if (count.intValue() > 0) {
      throw new BusinessException("帐号已经存在,请更换帐号名重试");
    }

    this.terminalType = "UPSTREAM";
    this.createTimeStamp = System.currentTimeMillis();
    this.enable = Short.valueOf("1");
    ExtBeanUtils.copyProperties(sto, this, false);
  }

  public void modify(UpAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    UpstreamReadRepository readRepository = (UpstreamReadRepository) SpringContextUtils.getBean(UpstreamReadRepository.class);
    UpAccountReadRepository accountReadRepository = (UpAccountReadRepository) SpringContextUtils.getBean(UpAccountReadRepository.class);
    Upstream upstream = readRepository.getOne(Wrapper.build().append(Restrictions.eq("id", mto.getBusinessEntityId())));
    //重置密码时获取数据库的账号需要预处理
    String suffix = "@" + upstream.getCode();
    if (mto.getLoginAccount().indexOf(suffix) > 0) {
      mto.setLoginAccount(mto.getLoginAccount().split(suffix)[0]);
    }
    //生成带code的账号
    StringBuffer upAccount = new StringBuffer(mto.getLoginAccount()).append(suffix);
    mto.setLoginAccount(upAccount.toString());
    //检查账号是否已存在
    Long count = accountReadRepository.getCountByAccountName(mto.getLoginAccount(), mto.getId());
    if (count.intValue() > 0) {
      throw new BusinessException("帐号已经存在,请更换帐号名重试");
    }

    if (this.enable.equals(Short.valueOf("-1"))) {//冻结将所属角色取消
      this.roleId = null;
    }
    this.lastUpdateTimeStamp = System.currentTimeMillis();
    ExtBeanUtils.copyProperties(mto, this, false);
  }

  public void bindingLoginPhone(Long phoneId) {
    if (this.loginPhoneId != null) {
      throw new BusinessException(this.loginAccount+"已绑定手机,请先解绑原手机再重新绑定");
    }
    this.loginPhoneId = phoneId;
  }

  public void untyingLoginPhone() {
    this.loginPhoneId = null;
  }

  public void changeEnable(Short enable) {
    this.enable = enable;
    /*if (this.enable.equals(Short.valueOf("-1"))) {//冻结将所属角色取消
      this.roleId = null;
    }
    this.lastUpdateTimeStamp = System.currentTimeMillis();*/
  }


  public void changeRole(Long roleId) {
    ExtAssert.notNull(roleId, "id为空");
    this.roleId = roleId;
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  public void destory() {
    if (this.admin.equals(Short.valueOf("1")))
      throw new BusinessException("主账号不可删除！");
    if (this.enable.equals(Short.valueOf("1"))) {
      throw new BusinessException("此账号正在使用中,不能删除");
    }
  }
}
